package 第三周;

/*
 * @author  YxinMiracle
 * @date  2022-04-27 16:33
 * @Gitee: https://gitee.com/yxinmiracle
 */

import java.util.ArrayList;
import java.util.List;

class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};


public class N叉树的前序遍历 {

    public List<Integer> ans;

    public List<Integer> preorder(Node root) {
        ans = new ArrayList<>();
        dfs(root);
        return ans;
    }

    private void dfs(Node root) {
        if (root == null) return;
        ans.add(root.val);
        for (int i = 0; i < root.children.size(); i++) {
            dfs(root.children.get(i));
        }
    }


}
